import Vue from "vue";
import App from "./App.vue";

//按需引入ElementUI
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

//按需全局使用ElementUI
Vue.use(ElementUI);

//引入路由
import router from "./router/index.js";

//引入vuex
import store from "./store/index.js";

//引入动画样式
import animated from "animate.css";
Vue.use(animated);

//引入mock
import "./api/mock";

//引入Cookie
import Cookie from "js-cookie";

//添加全局前置导航守卫
router.beforeEach((to, from, next) => {
  //判断token存不存在
  const token = Cookie.get("token");
  //token不存在，说明当前用户是没有登录
  if (!token && to.name !== "login") {
    next({ name: "login" });
  } else if (token && to.name === "login") {
    //token存在,说明用户登录，此时跳转首页
    next({ name: "home" });
  } else {
    next();
  }
});

//引入组件库
import jvuewheel from "@jyeontu/jvuewheel";
//引入样式
import "@jyeontu/jvuewheel/lib/jvuewhell.css";
Vue.use(jvuewheel);

//引入插件
import vueResource from "vue-resource";
//使用插件
Vue.use(vueResource);

Vue.config.productionTip = false;

new Vue({
  render: (h) => h(App),
  router,
  store,
  created() {
    store.commit("tab/addMenu", router);
  },
  beforeCreate() {
    Vue.prototype.$bus = this; //安装全局事件总线
  },
}).$mount("#app");
